package com.shenhan.assistclass;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.stream.IntStream;

/**
 * @Author shenhan
 * @Date: 2020/6/10 22:09
 * @Description:
 */
public class CyclicBarrierDemo {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> System.out.println("召唤神龙"));

        IntStream.range(1, 8).forEach(i -> new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "收集了" + i + "颗龙珠");
            try {
                cyclicBarrier.await();
                System.out.println(Thread.currentThread().getName() + "结束");
            } catch (InterruptedException | BrokenBarrierException e) {
                e.printStackTrace();
            }
        }, String.valueOf(i)).start());
    }
}
